package com.example.demo.modules.customDefine.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.R;
import com.example.demo.modules.customDefine.entity.ModuleFieldLongTextData;
import com.example.demo.modules.customDefine.service.ModuleFieldLongTextDataService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

import static com.example.demo.common.util.Utils.isNil;

@RestController
@RequestMapping("/api/moduleFieldLongTextData")
public class ModuleFieldLongTextDataController {

    @Autowired
    private ModuleFieldLongTextDataService moduleFieldLongTextDataService;


    @GetMapping("/listAll")
    public R listAll() {
        return R.ok().put("result",moduleFieldLongTextDataService.list(null));
    }

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",dataType="String"),
            @ApiImplicitParam(name="pageNum",dataType="String")
    })
    public R page(
            Integer pageSize,Integer pageNum) {
        if (isNil(pageSize)) {
            pageSize = 10;
        }
        if (isNil(pageNum)) {
            pageNum = 0;
        }
        QueryWrapper<ModuleFieldLongTextData> queryWrapper = new QueryWrapper<>();
        return R.ok().put("result",moduleFieldLongTextDataService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }
    @PostMapping("/add")
    public R add(@RequestBody ModuleFieldLongTextData moduleFieldLongTextData) {
        return R.ok().put("result",moduleFieldLongTextDataService.save(moduleFieldLongTextData));
    }
    @PostMapping("/update")
    public R update(@RequestBody  ModuleFieldLongTextData moduleFieldLongTextData, HashMap<String, Object> updateOptions) {
        QueryWrapper<ModuleFieldLongTextData> queryWrapper=new QueryWrapper<>();
        if(!isNil(moduleFieldLongTextData.getId())){
            queryWrapper.eq("id",moduleFieldLongTextData.getId());
        }else{
          return R.error("id不能为空");
        }
        return R.ok().put("result",moduleFieldLongTextDataService.update(moduleFieldLongTextData,queryWrapper));
    }
}
